/*
 * @Description: 大牌购物车
 * @Author: Kerwin
 * @Date: 2025-06-03 11:40:23
 * @LastEditTime: 2025-09-19 17:43:46
 * @LastEditors:  Please set LastEditors
 */

import { defineStore } from 'pinia'

export const useCartStore = defineStore('bigCar', {
  state: () => ({
    goodsList: [] as any[], // 当前购物车商品列表
    goodNum: 0 // 商品总数
  }),
  actions: {
    addGoods(data: any) {
      data.time = Date.now()
      const index = this.goodsList.findIndex((item: any) => {
        switch (data.shopBrandId) {
          case '13':
            if (!item.details?.otherStr || !data.details?.otherStr) {
              return item.shopProductSkuId === data.shopProductSkuId
            } else {
              return (
                item.shopProductSkuId === data.shopProductSkuId &&
                item.details.otherStr === data.details.otherStr
              )
            }
          default:
            return item.shopProductSkuId === data.shopProductSkuId
        }
      })

      if (index !== -1) {
        this.goodsList[index].num += data.num
      } else {
        this.goodsList.push(data)
      }

      this.goodNum = this.goodsList.reduce((sum, item) => sum + item.num, 0)
    },
    clearGoods() {
      this.goodsList = []
      this.goodNum = 0
    },
    reduceGoods(data: any) {
      const index = this.goodsList.findIndex((item: any) => {
        switch (data.shopBrandId) {
          case '13':
            if (!item.details?.otherStr || !data.details?.otherStr) {
              return item.shopProductSkuId === data.shopProductSkuId
            } else {
              return (
                item.shopProductSkuId === data.shopProductSkuId &&
                item.details.otherStr === data.details.otherStr
              )
            }
          default:
            return item.shopProductSkuId === data.shopProductSkuId
        }
      })

      if (index !== -1) {
        this.goodsList.splice(index, 1)
      }

      this.goodNum = this.goodsList.reduce((sum, item) => sum + item.num, 0)
    }
  },
  persist: true
})

export default useCartStore
